package com.lagou.cl.reduce;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class SortReduce extends Reducer<IntWritable, NullWritable, Text, NullWritable> {

	private static int countNum = 0;
	private static int maxNum = 0;
	private Text keyText = new Text();

	@Override
	protected void reduce(IntWritable key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
		//如果有相同的数字，则输入的 k<number>：v<null,null>，因此需要遍历values，如果不遍历，则会丢失数据
		for (NullWritable value : values) {
			int currentNum = key.get();
			if (currentNum > maxNum) {
				//将当前最大值赋值到临时变量中
				maxNum = currentNum;
			}

			//每遍历一次都需要加一
			countNum++;
			String out = countNum + "\t" + currentNum;
			keyText.set(out);
			context.write(keyText, NullWritable.get());
		}
	}
}
